package org.jeecg.modules.rental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.demo.rental.entity.PlatformProfits;

import org.jeecg.modules.rental.mapper.PlatformProfitsMapper;
import org.jeecg.modules.rental.service.IPlatformProfitsService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Description: 平台收益表
 * @Author: jeecg-boot
 * @Date:   2025-02-26
 * @Version: V1.0
 */
@Service
public class PlatformProfitsServiceImpl extends ServiceImpl<PlatformProfitsMapper, PlatformProfits> implements IPlatformProfitsService {
    @Override
    public List<Map<String, Object>> getPlatformIncomeStatistics(String type) {
        // 根据统计纬度生成时间格式
        String dateFormat;
        switch (type.toLowerCase()) {
            case "year":
                dateFormat = "%Y"; // 按年统计
                break;
            case "quarter":
                dateFormat = "%Y-Q%q"; // 按季度统计
                break;
            case "month":
                dateFormat = "%Y-%m"; // 按月统计
                break;
            case "week":
                dateFormat = "%Y-%u"; // 按周统计
                break;
            case "day":
                dateFormat = "%Y-%m-%d"; // 按日统计
                break;
            default:
                throw new IllegalArgumentException("不支持的统计纬度：" + type);
        }

        // 使用 MyBatis Plus 的 QueryWrapper 查询数据库
        QueryWrapper<PlatformProfits> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DATE_FORMAT(income_time, '" + dateFormat + "') AS `key`",
                        "SUM(income) AS `value`")
                .groupBy("DATE_FORMAT(income_time, '" + dateFormat + "')")
                .orderByAsc("`key`");

        // 查询平台收益数据
        List<Map<String, Object>> result = this.baseMapper.selectMaps(queryWrapper);

        return result;
    }
}
